package com.bosicen.crm.web.controller.sysmanage;


import java.io.IOException;
import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.bosicen.crm.dao.entity.Product;
import com.bosicen.crm.dao.entity.ProductSery;
import com.bosicen.crm.dao.entity.User;
import com.bosicen.crm.service.sysmanage.ProductServiceImpl;
import com.bosicen.crm.service.sysmanage.ProductSeryServiceImpl;
import com.bosicen.crm.tools.AppUtil;
import com.bosicen.crm.tools.CompressPic;
import com.bosicen.crm.tools.Page;
import com.google.gson.Gson;


/***
 * 商品
 * @author tigerlei
 *
 */
@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	ProductServiceImpl productServiceImpl;
	@Autowired
	public ProductSeryServiceImpl productSeryService;

	/**
	 * 新增,修改信息
	 * @param company
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/addProduct/{type}")
	public String addcompany(HttpServletRequest request,@PathVariable("type") Integer type){
		String id = request.getParameter("id");
		Product product = null;
//		User user=(User) request.getSession().getAttribute("userinfo");
//		ProductSery sery =new ProductSery();
//		sery.setCompanyid(user.getCompanyid());
//		sery.setPid(type);
//		List list = this.productSeryService.findProductSeryList(sery);
		if(type != 0 && StringUtils.isNotBlank(id)){
			product = productServiceImpl.findProductById(AppUtil.ObjectToInt(id));
		}
		request.setAttribute("product", product);
		//request.setAttribute("list", list);
		return "sysmanage/addproduct";
	}
	
	
	/**
	 * 保存新增/修改、删除信息
	 * 
	 * @param company
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/cudProduct/{type}")
	public void saveProduct(Product product, HttpServletRequest request, @PathVariable("type") Integer type, Writer writer) {
		boolean flag = false;
		Gson gson = new Gson();
		User user = (User) request.getSession().getAttribute("userinfo");
		product.setCompanyid(user.getCompanyid());
		try {
			switch (type) {
			case 0:
				flag = productServiceImpl.delProduct(product);
				break;
			case 1:
				flag = productServiceImpl.saveOrUpdateProduct(product);
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.write(gson.toJson(flag));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/productlist")
	public String findProductlist(Product product,HttpServletRequest request) {
		//String id = request.getParameter("id");
		User user=(User) request.getSession().getAttribute("userinfo");
		ProductSery sery =new ProductSery();
		sery.setCompanyid(user.getCompanyid());
		sery.setPid(0);
		List list = this.productSeryService.findProductSeryList(sery);
		if(product == null)
			product=new Product();
		Page page = new Page();
		page.setEveryPage(10);
		page.setCurrentPage(AppUtil.ObjectToInt(request.getParameter("pageno")));
		product.setCompanyid(user.getCompanyid());
		page = productServiceImpl.findProduct4Page(page, product);
		request.setAttribute("page",page);
		request.setAttribute("list", list);
		request.setAttribute("product",product);
		return "sysmanage/product_list";
	}
	/***
	 * 通过类型查询产品详细
	 * @param request
	 * @param type
	 * @param writer
	 */
	@RequestMapping(value="/findProductList/{type}")
	public void findProductList(HttpServletRequest request,@PathVariable("type") Integer type,Writer writer){
		 List<Product>  list=null;
		 Gson gson=new Gson();
		try{
			User user=(User) request.getSession().getAttribute("userinfo");
			Product product=new Product();
			product.setCompanyid(user.getCompanyid());
			product.setType(type);
			list=this.productServiceImpl.findProductList(product);
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			try {
				writer.write(gson.toJson(list));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 保存更新商品
	 * @param product
	 * @param request
	 * @param writer 
	 * @param file Spring 的MultipartFile文件上传
	 */
	@RequestMapping(value = "/cudProduct2")
	public void saveProductsssss(Product product,HttpServletRequest request,Writer writer,@RequestParam("fileInput") MultipartFile file) {
		boolean flag = false;
		Gson gson = new Gson();
		String path = "";
		String filename = System.currentTimeMillis()+"";
		try {
			if(product != null){
				if(file != null){
					path = ProductController.class.getResource("").getPath().split("WEB-INF")[0] + "upload/images/"; // 获取站点下的绝对路径
					path = path.substring(1,path.length());
					path += product.getCompanyid()+"/"+product.getType()+"/"+product.getFlag()+"/";
					//保存图片文件
					flag = CompressPic.proportionalSaveFile(file,path,filename);
				}
				if(product.getId() <= 0)
					product.setImage(filename);
				flag = productServiceImpl.saveOrUpdateProduct(product);
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		} finally {
			try {
				writer.write(gson.toJson(flag));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 保存更新商品
	 * @param product
	 * @param request
	 * @param writer 
	 * @param file Spring 的MultipartFile文件上传
	 */
	@RequestMapping(value = "/upload3")
	public ModelAndView saveProducts3(Product product ,HttpServletRequest request,@RequestParam("fileInput") MultipartFile file) {
		boolean flag = false;
		String path = "";
		String filename = System.currentTimeMillis()+"";
		User user = (User) request.getSession().getAttribute("userinfo");
		try {
			if(product != null){
				product.setCompanyid(user.getCompanyid());
				if(file != null && !file.isEmpty()){
					path = ProductController.class.getResource("").getPath().split("WEB-INF")[0] + "upload/images/"; // 获取站点下的绝对路径
					path = path.substring(1,path.length());
					path += product.getCompanyid()+"/"+product.getType()+"/"+product.getFlag()+"/";
					//保存图片文件
					flag = CompressPic.proportionalSaveFile(file,path,filename);
				}
				if(product.getId() <= 0)
					product.setImage(filename);
				flag = productServiceImpl.saveOrUpdateProduct(product);
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		} 
		return new ModelAndView("redirect:/product/productlist");
	}
}
